<?
define('ROOT', $_SERVER['DOCUMENT_ROOT']);
require_once (ROOT . '/public/db.php');
require_once (ROOT . '/public/json.php');
$pn = $_REQUEST['pageNo'];
$pn = isset ($pn) ? $pn : $_REQUEST['page'];
$pn *= 1;
$ps = $_REQUEST['pageSize'];
$ps = isset ($ps) ? $ps : $_REQUEST['limit'];
$ps *= 1;
if ($ps < 1)
	$ps = 50;
$pc = floor((156 + $ps -1) / $ps);
$pn = min(max(1, $pn), $pc);
$ql = 'select * from grid_data';
$ky = $_REQUEST['query'];
if (isset ($ky)) {
	if (get_magic_quotes_gpc()) {
		$ky = stripslashes($ky);
	} else {
		$ky = mysql_real_escape_string($ky);
	}
	$ql = $ql . " where vSubject like '%" . $ky . "%'";
}
$ob = $_REQUEST['orderBy'];
if (isset ($ob)) {
	$ob = str_replace('debit', 'iDebit', $ob);
	$ob = str_replace('credit', 'iCredit', $ob);
	$ob = str_replace('subject', 'vSubject', $ob);
	$ob = str_replace('summary', 'vSummary', $ob);
	$ob = str_replace('no', 'iNo', $ob);
	$ql .= ' order by ' . $ob;
}
$ql .= ' limit ' . (($pn -1) * $ps) . ',' . $ps;
$rs = mysql_query($ql);
$lt = array ();
while ($r = mysql_fetch_array($rs)) {
	$a = array ();
	$a['no'] = $r['iNo'];
	$a['summary'] = $r['vSummary'];
	$a['subject'] = $r['vSubject'];
	$a['debit'] = $r['iDebit'] * 1;
	$a['credit'] = $r['iCredit'] * 1;
	$lt[] = $a;
}
$dd = array (
	'success' => true,
	'data' => array (
		'list' => $lt,
		'pageNo' => $pn,
		'pageSize' => $ps,
		'pageCount' => $pc,
		'total' => 156
	)
);
echo $JSON->encode($dd);
?>